<template>
  <div>
    <el-dialog ref="dislogRef" @close="dialogVisibleClose" :visible.sync="dialogVisible" lock-scroll width="50%">
      <div slot="title" class="header_title">
        <span class="title_span">生成{{ billType }}单</span>
      </div>
      <div class="content_div">
        <div class="tips_text" v-if="createdStated">当前已选择{{ orderData.length }} 个执行状态为未完成的订单，系统将根据这些订单生成如下{{ companyCount
        }}个客户的{{ billType }}单</div>
        <p class="tips_text" v-else>系统已成功生成以下 {{ tableResult.length }} 个{{ billType == '采购' ? '采购入库' : '销售出库' }}单</p>
        <div class="info_table">
          <div class="div_table">
            <div class="table_content" v-if="createdStated">
              <el-table :data="orderData" height="100%">
                <el-table-column min-width="50" type="index" label="序号"></el-table-column>
                <el-table-column min-width="50" property="companyId_dictText" label="往来单位"></el-table-column>
                <el-table-column min-width="100" property="notArrivalAmount" label="未完金额"></el-table-column>
              </el-table>
            </div>
            <div class="table_content" v-else>
              <el-table :data="tableResult" height="100%">
                <el-table-column min-width="50" type="index" label="序号"></el-table-column>
                <el-table-column min-width="150" property="orderNumber" label="单据编号"></el-table-column>
                <el-table-column min-width="120" property="xghStorehouseId_dictText" label="仓库"></el-table-column>
                <el-table-column min-width="120" property="xghCompanyId_dictText" label="往来单位"></el-table-column>
                <el-table-column min-width="80" property="receivablePrice" label="金额"></el-table-column>
              </el-table>
            </div>
          </div>
        </div>
        <div class="info_button" v-if="createdStated">
          <el-button type="primary" @click.stop="createdOrder" style="width: 80px">确认生成</el-button>
          <el-button @click.stop="dialogVisible = false">关闭</el-button>
        </div>
        <div class="info_button" v-else>
          <el-button @click.stop="dialogVisible = false">取消</el-button>
        </div>
      </div>
      <!-- 提醒弹框 -->
    </el-dialog>
    <PopupReminder ref="reminderRef" :tipsData="tipsData" @onConfirm="onYes"></PopupReminder>
  </div>
</template>

<script>
import PopupReminder from '@/views/components/popupReminder'
import { getAction } from '../../api/manage'
import { formatMoney } from '../../utils'
export default {
  name: 'dialogOperationLog',
  components: {
    PopupReminder,
  },
  props: {
    orderData: Array,
    billType: String,
  },
  data() {
    return {
      // 传递给弹框的数据
      tipsData: {
        tipsWords: '',
        tipsMark: '',
      },
      dialogVisible: false, // 新增弹框
      createdStated: true,
      companyCount: 1,
      tableResult: [],
    }
  },
  watch: {
    dialogVisible(val) {
      if (val) {
        let list = []
        for (let index = 0; index < this.orderData.length; index++) {
          const element = this.orderData[index];
          let companyId = element.companyId
          if (list.indexOf(companyId) == -1) {
            list.push(companyId)
          }
        }
        this.companyCount = list.length
      }
    }
  },
  methods: {
    //   确认生成
    createdOrder() {
      if (this.billType == '销售') {
        this.tipsData.tipsWords = `确定要生成${this.billType}出库单吗？`
      } else if (this.billType == '采购') {
        this.tipsData.tipsWords = `确定要生成${this.billType}入库单吗？`
      }
      this.tipsData.tipsMark = 'onSure'
      this.$refs.reminderRef.dialogVisible = true
    },
    // 提醒弹框确认后操作
    onYes(mark) {
      if (mark == 'onSure') {
        // this.createdStated = false
        if (this.billType == '采购') {
          this.createPurchaseBill()
        } else {
          this.createSaleBill()
        }
      }
    },
    dialogVisibleClose() {
      this.createdStated = true
      this.dialogVisible = false
    },
    createPurchaseBill() {
      let list = []
      for (let index = 0; index < this.orderData.length; index++) {
        const element = this.orderData[index];
        list.push(element.orderNumber)
      }
      getAction('/xghBuyOrder/xghBuyOrder/addBuyOrderByPurchaseOrderNumbers', { purchaseOrderNumbers: list.toString() }).then(res => {
        console.log(res);
        if (res.success) {
          this.createdStated = false
          let list = res.result || []
          let result = []
          for (let index = 0; index < list.length; index++) {
            const element = list[index];
            let orderObj = element.buyOrderPage
            result.push({
              orderNumber: orderObj.orderNumber,
              xghStorehouseId_dictText: orderObj.xghStorehouseId_dictText,
              xghCompanyId_dictText: orderObj.xghCompanyId_dictText,
              receivablePrice: formatMoney(orderObj.receivablePrice)
            })
          }
          this.tableResult = result
        } else {
          this.$elMessage.error(res.message)
        }
      })
    },
    createSaleBill() {
      let list = []
      for (let index = 0; index < this.orderData.length; index++) {
        const element = this.orderData[index];
        list.push(element.orderNumber)
      }
      getAction('/xghSaleStorehouseOrder/xghSaleStorehouseOrder/addSaleOrderByOrderNumbers', { orderNumbers: list.toString() }).then(res => {
        console.log(res);
        if (res.success) {
          this.createdStated = false
          let list = res.result || []
          let result = []
          for (let index = 0; index < list.length; index++) {
            const element = list[index];
            let orderObj = element.saleStorehouseOrderPage
            result.push({
              orderNumber: orderObj.orderNumber,
              xghStorehouseId_dictText: orderObj.xghOutStorehouseId_dictText,
              xghCompanyId_dictText: orderObj.xghCompanyId_dictText,
              receivablePrice: formatMoney(orderObj.receivablePrice)
            })
          }
          this.tableResult = result
        } else {
          this.$elMessage.error(res.message)
        }
      })
    },
  },
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog {
  width: 100%;
  margin-top: 20vh !important;

  .el-dialog__body {
    padding: 0;
  }

  .el-dialog__header {
    width: 100%;
    height: 60px;
    box-sizing: border-box;
    padding: 0 0.187512rem 0 0.281268rem;
    border-bottom: 1px solid #eeeeee;
    display: flex;
    align-items: center;
    justify-content: space-between;

    .el-dialog__headerbtn {
      position: static;

      .el-icon-close {
        color: #999;
        font-size: 0.48753rem;
      }
    }
  }

  .header_title {
    width: 100%;
    display: flex;
    align-items: center;

    .title_span {
      white-space: nowrap;
      font-size: 0.300019rem;
      color: #666;
      font-weight: 600;
    }
  }
}

.content_div {
  width: 100%;
  height: 50vh;
  background: #fff;
  box-sizing: border-box;
  padding: 0 0.281268rem;

  .info_table {
    width: 100%;
    height: calc(100% - 90px - 60px);
    display: flex;

    .div_table {
      width: 100%;
      height: 100%;
      display: flex;
      flex-direction: column;
      justify-content: space-between;

      .table_content {
        height: 100%;

        ::v-deep .el-table {
          border: 1px solid #e3e3e3;
          box-sizing: border-box;

          th,
          td {
            padding: 0;
            color: #666;
            box-sizing: border-box;
            border-right: 1px solid #e3e3e3;

            .cell {
              height: 30px;
              display: flex;
              align-items: center;
              font-size: 12px;
            }

            &:first-child {
              .cell {
                justify-content: center;
              }
            }
          }

          th {
            .cell {
              background: #ededed;
              justify-content: center;
            }
          }

          .el-table__body tr:hover>td {
            background: #e3f6ff;
          }

          .el-table__footer-wrapper {
            td {
              background: #f4f4f4;
              border: none;
            }
          }

          //滚动条的宽度
          ::-webkit-scrollbar {
            width: 10px;
            height: 10px;
            background-color: #e5e5e5;
          }

          //滚动条的滑块
          ::-webkit-scrollbar-thumb {
            background-color: #c9c9c9;
          }
        }
      }

      .total_record {
        width: 100%;
        height: 0.562535rem;
        box-sizing: border-box;
        padding: 0 0.281268rem;
        border: 1px solid #e3e3e3;
        border-top: none;
        background: #f4f4f4;
        display: flex;

        p {
          margin: 0;
          line-height: 0.562535rem;
          padding-right: 0.281268rem;
          font-size: 0.225rem;
          font-family: PingFang SC;
          font-weight: 500;
          color: #666666;
        }
      }
    }
  }

  .tips_text {
    width: 100%;
    height: 60px;
    line-height: 60px;
    margin: 0;
    font-size: 14px;
    font-family: PingFang SC;
    font-weight: 400;
    color: #1993fb;
  }

  .info_button {
    width: 100%;
    height: 0.750047rem;
    height: 0.937559rem;
    height: 90px;
    display: flex;
    align-items: center;
    justify-content: flex-end;

    ::v-deep .el-button {
      width: 1.12507rem;
      height: 0.562535rem;
      display: flex;
      justify-content: center;
      align-items: center;
      border-radius: 0.093756rem;
      border-color: #dddddd;
      color: #666;
      font-size: 12px;
    }

    ::v-deep .el-button--primary {
      background-color: #1890ff;
      color: #fff;
      border-color: #1890ff;
    }
  }
}</style>